package com.hzhr.iot.core.exception;

import com.hzhr.iot.core.constant.ServiceErrors;

import java.util.Map;

public class ServiceException extends  Exception {
    private ServiceErrors serviceErrors;
    private Map<String, Object> objectInfo;
    private Throwable cause;

    public ServiceException(ServiceErrors serviceErrors) {
        this.serviceErrors = serviceErrors;
    }
    public ServiceException(ServiceErrors serviceErrors, String message) {
        serviceErrors.setMessage(message);
        this.serviceErrors = serviceErrors;
    }
    public ServiceException(ServiceErrors serviceErrors, Throwable cause) {
        this.serviceErrors = serviceErrors;
        this.cause = cause;
    }
    public ServiceException(ServiceErrors serviceErrors, Map<String, Object> objectInfo) {
        this.serviceErrors = serviceErrors;
        this.objectInfo = objectInfo;
    }
    public ServiceException(ServiceErrors serviceErrors, String message, Map<String, Object> objectInfo) {
        serviceErrors.setMessage(message);
        this.serviceErrors = serviceErrors;
        this.objectInfo = objectInfo;
    }
    public ServiceException(ServiceErrors serviceErrors, Map<String, Object> objectInfo, Throwable cause) {
        this.serviceErrors = serviceErrors;
        this.objectInfo = objectInfo;
        this.cause = cause;
    }

    public ServiceErrors getServiceErrors() {
        return serviceErrors;
    }

    public Object getObjectInfo() {
        return objectInfo;
    }

    @Override
    public Throwable getCause() {
        return cause;
    }

    public String getCode() {
        if (serviceErrors == null || serviceErrors.getCode() == null) return null;
        return serviceErrors.getCode().toString();
    }
}
